package edu.nju.desserthouse.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.edu.desserthouse.model.Shop;

import edu.nju.desserthouse.dao.ShopDao;
/**
 * Servlet implementation class AddShopServlet
 */
@WebServlet("/addshop.technician")
public class AddShopServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	ShopDao shopDao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddShopServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		int id = (int) (shopDao.getShopNum() + 1);
		String address = req.getParameter("address").toString();
		
		Shop shop = new Shop();
		shop.setId(id);
		shop.setAddr(address);
		shopDao.save(shop);
		
		resp.sendRedirect("/desserthouseWEB/technician/addshop.jsp");
	}
}
